

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;

public class GrammarTest {
	
	public static void printHelp() {
		System.out.println("Pomoc:");
		System.out.println("\tWywołanie: java -jar pascal4j.jar src.pas [out.j] [-ch]");
		System.out.println("Opcje:");
		System.out.println("\t-c - wyjście w postaci wyłącznie komentarzy (domyślnie Jasmin)");
		System.out.println("\t-h - wyświetl pomoc");
	}
	
	public static void main(String []args) throws Exception {
		if(args.length < 1) {
			System.out.println("Podaj nazwe pliku źródłowego jako argument translatora!");
			System.exit(1);
		}
		FileInputStream src=null;
		try {
			if(args[0].equals("-h")) {
				printHelp();
				System.exit(1);
			}
			src=new FileInputStream(args[0]);
		} catch(FileNotFoundException e) {
			System.out.println("Plik źródłowy "+args[0]+" nie został znaleziony.");
			System.exit(1);
		}
		String outName="pascal4j.out";
		String template="Jasmin.stg";
		boolean out=false;
		for(int i=1;i<args.length;i++) {
			if(args[i].equals("-c")) {
				template="Comments.stg";
			} else if(args[i].equals("-h")) {
				printHelp();
				System.exit(1);
			} else if(out) {
				System.out.println("Niewłaściwa opcja "+args[i]);
				System.exit(1);
			} else if(!args[i].startsWith("-")){
				outName=args[i];
				out=true;
			}
		}
		
		// load the group file ByteCode.stg, put in templates var
		FileReader groupFileR = new FileReader(template);
		StringTemplateGroup templates =new StringTemplateGroup(groupFileR);
		groupFileR.close();

		//System.out.println("Wczytalem template");
		
		// Create an input character stream from standard in
		ANTLRInputStream input = new ANTLRInputStream(src);
		// Create an ExprLexer that feeds from that stream
		pascalLexer lexer = new pascalLexer(input);
		// Create a stream of tokens fed by the lexer
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		
		//System.out.println("Po przebiegu leksera");
		
		// Create a parser that feeds off the token stream
		pascalParser parser = new pascalParser(tokens);
		pascalParser.program_return r = parser.program();
		CommonTree t = (CommonTree)r.getTree();
		
		//System.out.println("Po pierwszym przebiegu parsera");
		//System.out.println(t.toStringTree());
		
		
		// Walk resulting tree; create treenode stream first
		CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
		// AST nodes have payloads that point into token stream
		nodes.setTokenStream(tokens);
		// Create a tree Walker attached to the nodes stream
		pascalWalker walker = new pascalWalker(nodes);
		walker.setTemplateLib(templates);
		// Invoke the start symbol, rule program
		pascalWalker.program_return ret=walker.program();
		
		//System.out.println("Po przebiegu parsera");
		
		// EMIT BYTE CODES
		// get template from return values struct
		StringTemplate output = (StringTemplate)ret.getTemplate();
		System.out.println(output.toString()); // render full template
		src.close();
	}
}
